// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

// Code generated by "make store-layers"
// DO NOT EDIT

package timerlayer

import (
	"context"
	timemodule "time"
	
    "github.com/mattermost/mattermost-server/v5/einterfaces"
	"github.com/mattermost/mattermost-server/v5/model"
	"github.com/mattermost/mattermost-server/v5/store"
)

type {{.Name}} struct {
	store.Store
	Metrics einterfaces.MetricsInterface
{{range $index, $element := .SubStores}}	{{$index}}Store store.{{$index}}Store
{{end}}
}

{{range $index, $element := .SubStores}}func (s *{{$.Name}}) {{$index}}() store.{{$index}}Store {
	return s.{{$index}}Store
}

{{end}}

{{range $index, $element := .SubStores}}type {{$.Name}}{{$index}}Store struct {
	store.{{$index}}Store
	Root *{{$.Name}}
}

{{end}}

{{range $substoreName, $substore := .SubStores}}
{{range $index, $element := $substore.Methods}}
func (s *{{$.Name}}{{$substoreName}}Store) {{$index}}({{$element.Params | joinParamsWithType}}) {{$element.Results | joinResultsForSignature}} {
	start := timemodule.Now()
	{{if $element.Results | len | eq 0}}
	s.{{$substoreName}}Store.{{$index}}({{$element.Params | joinParams}})
	{{else}}
	{{$element.Results | genResultsVars}} := s.{{$substoreName}}Store.{{$index}}({{$element.Params | joinParams}})
	{{end}}
	elapsed := float64(timemodule.Since(start)) / float64(timemodule.Second)
	if s.Root.Metrics != nil {
		success := "false"
		if {{$element.Results | errorToBoolean}} {
			success = "true"
		}
		s.Root.Metrics.ObserveStoreMethodDuration("{{$substoreName}}Store.{{$index}}", success, elapsed)
	{{ with ($element.Results | genResultsVars) -}}
	}
	return {{ . }}
	{{- else -}}
	}
	{{- end }}
}
{{end}}
{{end}}

{{range $index, $element := .Methods}}
func (s *{{$.Name}}) {{$index}}({{$element.Params | joinParamsWithType}}) {{$element.Results | joinResultsForSignature}} {
	{{if $element.Results | len | eq 0}}s.Store.{{$index}}({{$element.Params | joinParams}})
	{{else}}return s.Store.{{$index}}({{$element.Params | joinParams}})
	{{end}}}
{{end}}

func New(childStore store.Store, metrics einterfaces.MetricsInterface) *{{.Name}} {
	newStore := {{.Name}}{
		Store: childStore,
		Metrics: metrics,
	}
	{{range $substoreName, $substore := .SubStores}}
	newStore.{{$substoreName}}Store = &{{$.Name}}{{$substoreName}}Store{{"{"}}{{$substoreName}}Store: childStore.{{$substoreName}}(), Root: &newStore}{{end}}
	return &newStore
}
